﻿using System.Collections.Generic;
using System.Runtime.Serialization;
using GMME.Model.Rtm.Notify;
using GMME.Util;
using UnityEngine;
using UnityEngine.Scripting;

namespace GMME.Model.Rtm.Result
{
    [DataContract]
    public class GetRtmChannelHistoryMessagesResult : ErrorResult
    {
        [DataMember(Name = "channelId")]
        [Preserve]
        public string ChannelId { get; set; }

        [DataMember(Name = "channelMessages")]
        [Preserve]
        public List<RtmChannelHistoryMessageNotify> ChannelMessages { get; set; }

#if UNITY_ANDROID
        internal static GetRtmChannelHistoryMessagesResult ConvertGetRtmChannelHistoryMessagesResult(
            AndroidJavaObject javaObject)
        {
            if (javaObject != null)
            {
                GetRtmChannelHistoryMessagesResult getRtmChannelHistoryMessagesResult = new();
                getRtmChannelHistoryMessagesResult.ChannelId = javaObject.Call<string>("getChannelId");
                getRtmChannelHistoryMessagesResult.Code = javaObject.Call<int>("getCode");
                getRtmChannelHistoryMessagesResult.Msg = javaObject.Call<string>("getMsg");
                getRtmChannelHistoryMessagesResult.ChannelMessages = new List<RtmChannelHistoryMessageNotify>();
                var list =
                    TransferUtil.JavaListToCsharpList(
                        javaObject.Call<AndroidJavaObject>("getChannelMessages"));
                foreach (var element in list)
                {
                    var rtmChannelHistoryMessage =
                        RtmChannelHistoryMessageNotify.ConvertRtmChannelHistoryMessage(element);
                    getRtmChannelHistoryMessagesResult.ChannelMessages.Add(rtmChannelHistoryMessage);
                }

                return getRtmChannelHistoryMessagesResult;
            }

            return null;
        }
#endif
    }
}